<?php

/**
 * Error Handler for MirrorPress
 *
 * Based on WP_Error from WordPress and using the Singleton class from Habari
 * @author WordPress
 * @license GNU General Public License (see LICENSE.GPL.txt in the MirrorPress directory)
 */
class ErrorHandler extends Singleton {
	/**
	 * @var array List of errors
	 */
	private $errors = array();

	/**
	 * @var array Latest error
	 */
	private $latest;

	/**
	 * Enables singleton working properly
	 *
	 * @see singleton.php
	 */
	public static function instance()
	{
		return self::getInstanceOf( get_class() );
	}

	public static function add($code = '', $message = '', $data = '') {
		if(empty($code))
			return;
		ErrorHandler::instance()->errors[$code] = $latest = new Error($code, $message, $data);
		ErrorHandler::instance()->latest = $latest;
		return $latest;
	}

	public static function get($code = '') {
		if( empty($code) || !isset(ErrorHandler::instance()->errors[$code]) )
			return false;
		return ErrorHandler::instance()->errors[$code];
	}

	public static function get_latest() {
		if( empty(ErrorHandler::instance()->latest) )
			return false;
		return ErrorHandler::instance()->latest;
	}

	public static function is_error($error) {
		if(is_object($error) && $error instanceof Error)
			return true;
		return false;
	}
}